// 新增一个其它物质成本
function addOtherMaterial() {
	var num_of_other_material = $('input[name="other_material_description[]"]').length + 1; // 即将添加的其它物质成本编号

	// 添加行的HTML
	var trOtherMaterial = '<tr id="other-material-'+num_of_other_material+'">'+
		'<td>'+num_of_other_material+'</td>'+
		'<td><input type="text" name="other_material_description[]" value="" id="other-material-description-'+num_of_other_material+'" class="input-small validate[required]" autocomplete="off"></td>'+
		'<td><div class="input-append"><input type="text" name="other_material_cost[]" value="" id="other-material-cost-'+num_of_other_material+'" class="input-mini validate[required, custom[number], min[0]]" autocomplete="off"><span class="add-on">元</span></div></td>'+
	'</tr>';

	// 增加一行"其它物质"
	$("#other-materials").append(trOtherMaterial);

}

// 减少一个其它物质成本
function removeOtherMaterial(){
	var num_of_other_material = $('input[name="other_material_description[]"]').length + 1; // 即将添加的其它物质成本编号
 	$('#other-material-'+(num_of_other_material - 1)).remove();
}

// 验证其它物质名填写
function validateOtherMaterial(){
	if($('no_cost_other_material').prop("checked", false))
		return _validateOtherMaterialDup();
	return true;
}

// 验证重复[其它物质名]
function _validateOtherMaterialDup(){
	var description_inputs = $('input[name="other_material_description[]"]');
	var cost_inputs = $('input[name="other_material_cost[]"]');
	var total_other_num = description_inputs.length;

	var strAry = [];
	var dup_index = 0;
	for (var i = 0; i < total_other_num; i++) {
		if(description_inputs[i].value != '' && cost_inputs[i].value != '')
			strAry[i] = description_inputs[i].value;
	};

	for (var i = 0; i < strAry.length; i++) {
		for (var j = i + 1; j < strAry.length; j++)
			if(strAry[i] == strAry[j]) {
				dup_index = j; break;
			}
	};

	if(dup_index != 0) {
		// alert("first duplicate index: "+dup_index+", total_other_num "+total_other_num);
		$(description_inputs[dup_index]).validationEngine('showPrompt', "* 已有重复的项目存在", "error", true);
		return false;
	}
	return true;
}

// 验证当前其它物质信息是否填写完全
function _validateOtherMaterialGroupEmpty(){

	var description_inputs = $('input[name="other_material_description[]"]');

	var total_other_num = description_inputs.length;

	for(var i = 1; i <= total_other_num; i++) {
		description_is_valid = !($('[id="other-material-description-' + i + '"]').validationEngine('validate')); // 验证单项失败返回true
		cost_is_valid = !($('[id="other-material-cost-' + i + '"]').validationEngine('validate'));
		if ((description_is_valid && cost_is_valid) == false) {
			// 有未填项
			return false; // 有空缺项返回false
		};
	}
	return true; // 填写完整返回true
}
